<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"
export default {
  name: 'ThreeLineChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    headTitle: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    chartTime: {
      type: Array,
      required: true
    },
    interval: {
      type: Number,
      required: true
    },
    unit: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
          this.chartTime,
          this.interval,
          this.unit)
      }
    }
  },
  mounted() {
    window.addEventListener('resize', this.resizeHandler)
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeHandler)
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.headTitle, this.chartData, this.chartTime, this.interval, this.unit)
    },
    setOptions(title, chartData, chartTime, interval, unit) {
      this.chart.setOption({
        title: {
          text: title,
          left: 'center'
        },
        grid: {
          left: 20,
          right: 35,
          bottom: 30,
          top: 35,
          containLabel: true
        },
        xAxis: {
          data: chartTime.map(function (str) {
            return str.replace(' ', '\n');
          }),
          boundaryGap: false, //falseX轴两边对齐
          //坐标轴刻度相关设置
          axisTick: {
            //是否显示坐标轴刻度
            show: false
          }
        },
        yAxis: {
          type: "value",
          name: "单位：" + unit,
          nameLocation: "end",
          //min: 0,
          //max: yMax,
          //interval: interval,
          boundaryGap: false,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true
          },
          /*axisLabel: {
            formatter: '{value} ' + unit
          }*/
          axisLabel: {
            //加上这个会造成Y轴刻度间隔变小
            /*formatter: function (value) {
              return parseInt(value)
            }*/
          }
        },
        //提示框组件
        tooltip: {
          //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
          trigger: 'axis',
          //坐标轴指示器配置项
          axisPointer: {
            //十字准星指示器
            type: "line",//shadow阴影效果
            axis: 'x',
            //坐标轴指示器的文本标签
            label: {
              //文本标签中数值的小数点精度
              precision: 0
            }
          },
          //formatter: '{b0}<br />{a0}：{c0}' + unit +'<br/>{a1}：{c1}'+ unit +'<br/>{a2}：{c2}'+ unit,
          padding: [5, 10]
        },
        series: [
          {
            name: title,
            smooth: true,     // 这个可以显示为平滑过渡的线条
            symbol: "circle", // 这个把节点的空心圆换成实心圆
            symbolSize: 1,    // 节点大小，数字随意，跟下面的线宽一样或少 1 像素
            type: 'line',
            //线条样式
            lineStyle: {
              type: "dotted"
            },
            data: chartData,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          }
        ]
      })
    }
  }
}
</script>

<style scoped>
</style>
